package com.sky.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: RelationUserInfo
 *
 * @AUTHOR: zhang
 * DATE: 2024/1/23
 * DESCRIPTION :
 */
@Builder
@Data
public class RelationUserInfo {
    private Long userInfoId;
    private String address;
    private String bankCardNumber;


    public static void main(String[] args) {
        ArrayList<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(
                UserInfo.builder().id(111L).name("张三").age(23).gender(1).build()
        );
        userInfoList.add(
                UserInfo.builder().id(112L).name("李四").age(24).gender(1).build()
        );
        userInfoList.add(
                UserInfo.builder().id(113L).name("王五").age(21).gender(2).build()
        );
        System.out.println(userInfoList);


        ArrayList<RelationUserInfo> relationUserInfoList = new ArrayList<>();
        relationUserInfoList.add(
                RelationUserInfo.builder().userInfoId(112L).address("海南省三亚市天涯区天涯区直辖村级区划").bankCardNumber("420201197911039924").build()
        );
        relationUserInfoList.add(
                RelationUserInfo.builder().userInfoId(111L).address("贵州省黔南布依族苗族自治州龙里县洗马镇").bankCardNumber("610201198701219644").build()
        );
        relationUserInfoList.add(
                RelationUserInfo.builder().userInfoId(113L).address("北京市市辖区顺义区双丰街道").bankCardNumber("140201199704221100").build()
        );
        System.out.println(relationUserInfoList);


        Map<Long, RelationUserInfo> map = relationUserInfoList.stream().collect(Collectors.toMap(RelationUserInfo::getUserInfoId, c -> c, (k1, k2) -> k1.getUserInfoId() > k2.getUserInfoId() ? k1 : k2));


//
//
//        HashMap<Long, RelationUserInfo> map = new HashMap<>();
//        for (RelationUserInfo relationUserInfo : relationUserInfoList) {
//            map.put(relationUserInfo.getUserInfoId(), relationUserInfo);
//        }
//
//

        ArrayList<UserVO> userVOArrayList = new ArrayList<UserVO>();

        for (UserInfo userInfo : userInfoList) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userInfo, userVO);

            Long id = userVO.getId();
            RelationUserInfo relationUserInfo = map.get(id);

            userVO.setAddress(relationUserInfo.getAddress());
            userVO.setBankCardNumber(relationUserInfo.getBankCardNumber());
            userVOArrayList.add(userVO);
        }
       /* for (UserInfo userInfo : userInfoList) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userInfo, userVO);
            for (RelationUserInfo relationUserInfo : relationUserInfoList) {
                Long id = userVO.getId();
                Long userInfoId = relationUserInfo.getUserInfoId();
                if (id.equals(userInfoId)) {
                    userVO.setAddress(relationUserInfo.getAddress());
                    userVO.setBankCardNumber(relationUserInfo.getBankCardNumber());
                    userVOArrayList.add(userVO);
                }
            }
        }*/


        for (UserVO userVO : userVOArrayList) {
            System.out.println(userVO);
        }

    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class UserVO {
        private String address;
        private String bankCardNumber;

        private Long id;
        private String name;
        private Integer age;
        private Integer gender;

    }
}
